﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace Database
{
    [DataContract]
    public class Thread 
    {
        [DataMember]
        private int id;
        [DataMember]
        private string title;
        [DataMember]
        private string body;
        [DataMember]
        private DateTime creationTime;
        [DataMember]
        private User user;
        [DataMember]
        public SortedDictionary <int, Post> posts;
        [DataMember]
        private int forumId;
        [DataMember]
        private int subForumId;
        [DataMember]
        private int idForPosts;
       
       

        public Thread(int id, string title, string body, User user, int forumId, int subForumId) {
            this.id = id;
            this.title = title;
            this.body = body;
            this.creationTime = DateTime.Now;
            this.user = user;
            this.posts = new SortedDictionary<int, Post>();
            this.forumId = forumId;
            this.subForumId = subForumId;
            this.idForPosts = 0;
        }

        public void setIdForPosts(int idForPosts)
        {
            this.idForPosts = idForPosts;
        }
        
        public int getId() {
            return this.id;
        }

        public string getTitle() {
            return this.title;
        }

        public void setTitle(string title)
        {
            this.title = title;
        }

        public string getBody()
        {
            return this.body;
        }

        public void setBody(string body)
        {
            this.body = body;
        }

        public User getUser()
        {
            return this.user;
        }

        public DateTime getTime() {
            return this.creationTime;
        }
        public void setTime(DateTime time)
        {
            this.creationTime = time;
        }

        public SortedDictionary <int, Post> getPosts() {
            return this.posts;
        }

        public void setPosts(SortedDictionary<int, Post> posts)
        {
            this.posts = posts;
        }
        
        public Post getPost(int postId) {
            if (posts.ContainsKey(postId))
                return posts[postId];
            else
                return null;
        }

        public bool removePost(int postId)
        {
            if (posts.ContainsKey(postId))
                return posts.Remove(postId);
            else 
                return false;
        }

        //in success return post id.
        public int addPost(string title, string body, User user) {
                idForPosts += 1;
                Post post = new Post(idForPosts, title, body, user, forumId, subForumId, id);
                posts.Add(idForPosts, post);
                return idForPosts;
        }

        public int getForumId() {
            return this.forumId;
        }

        public int getSubForumId()
        {
            return this.subForumId;
        }

        public int getNumOfPosts()
        {
            return this.posts.Count;
        }

        //return SortedDictionary <int, string>  int - id, string - (title of thread + boby of thread + user) + (post title + post body + posted user -> "title: body. [user]")
        public SortedDictionary<int, string> threadToString()
        {
            
            SortedDictionary<int, string> titles = new SortedDictionary<int, string>();
            string title = this.title;
            string body = this.body;
            string uname = this.user.getUserName();
            titles.Add(0, (title + ": " + body + ". [" + uname + "]"));

                foreach (int id in posts.Keys)
                {
                    string title1 = posts[id].getTitle();
                    string body1 = posts[id].getBody();
                    string uname1 = posts[id].getUser().getUserName();
                    titles.Add(id, (title1 + ": " + body1 + ". [" + uname1 + "]"));
                }
                return titles;
 
        }

    }
}
